﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;

namespace ASPNETPerformance.App_BusinessLogic.Cache {
    public class DataCacheManager {
        private readonly System.Web.Caching.Cache _cache;

        public DataCacheManager() {
            HttpContext current = HttpContext.Current;
            if ( current != null ) {
                _cache = current.Cache;
            }
            else {
                _cache = HttpRuntime.Cache;
            }
        }

        #region 方法

        /// <summary>
        /// Removes all keys and values from the cache
        /// </summary>
        public void Clear() {
            IDictionaryEnumerator enumerator = _cache.GetEnumerator();
            while ( enumerator.MoveNext() ) {
                _cache.Remove( enumerator.Key.ToString() );
            }
        }

        /// <summary>
        /// Gets or sets the value associated with the specified key.
        /// </summary>
        /// <param name="key">The key of the value to get.</param>
        /// <returns>The value associated with the specified key.</returns>
        public object Get( string key ) {
            return _cache[ key ];
        }

        /// <summary>
        /// Adds the specified key and object to the cache.
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="obj">object</param>
        public void Add( string key, object obj ) {
            Add( key, obj, null );
        }

        /// <summary>
        /// Adds the specified key and object to the cache.
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="obj">object</param>
        /// <param name="dep">cache dependency</param>
        public void Add( string key, object obj, CacheDependency dep ) {
            if ( obj != null ) {
                _cache.Insert( key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null );
            }
        }

        /// <summary>
        /// Removes the value with the specified key from the cache
        /// </summary>
        /// <param name="key"></param>
        public void Remove( string key ) {
            _cache.Remove( key );
        }

        /// <summary>
        /// Removes items by pattern
        /// </summary>
        /// <param name="pattern">pattern</param>
        public void RemoveByPattern( string pattern ) {
            IDictionaryEnumerator enumerator = _cache.GetEnumerator();
            Regex regex = new Regex( pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase );
            while ( enumerator.MoveNext() ) {
                if ( regex.IsMatch( enumerator.Key.ToString() ) ) {
                    _cache.Remove( enumerator.Key.ToString() );
                }
            }
        }

        #endregion
    }
}